/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
#include <QRegExp>
#include "line.h"
#include "terminal.h"
#include "tagcache.h"

const char *const Line::typeNames[LINE_TYPE_COUNT] = {
	"...", "SAY", "TEL", "SHT", "HUG", "KIS", "MOV",
 	"FAU", "SYS", "GOS", "LOG", "AFK", "CMD", "ACT"
};

void Line::determineType(TagCache &tagCache)
{	
	Type typeHint = Unknown;
	
	bool BEL = m_binary.contains('\007');
	
	if (m_binary.startsWith("\e[0m\e[1m\e[33m")) {
		typeHint = Shout;
	}
	
	if (
			m_binary.startsWith("\e[0m\e[1m\e[31m")
		||	m_binary.startsWith("\e[0m\e[0m\e[1m\e[31m")
	) {
		if (m_plain.contains("sa ti prave odhlasil")) {
			typeHint = Login;
		} else {
			typeHint = Gossip;
		}
	}
	
	if (
			m_binary.startsWith("\e[0m\e[36m")
		||	m_binary.startsWith("\e[0m\e[0m\e[36m")
	) {
		if (m_plain.startsWith("Akt. TEMA: ")) {
			/* Hack: Aktualna tema byva zamienana za emote kvoli farbe */
			typeHint = Command;
		} else {
			typeHint = Say;
		}
	}
	
	m_tag = tagCache.getTag(m_plain, typeHint, BEL);
	m_tag.BEL = BEL;
	return;
}

const QString &Line::object() const
{
	return m_object;
}

const QString &Line::subject() const
{
	return m_subject;
}

const Line::Tag &Line::tag() const
{
	return m_tag;
}

const QByteArray &Line::binary() const
{
	return m_binary;
}

const QString &Line::plain() const
{
	return m_plain;
}

Line::Tag::Tag()
{
	type = Line::Unknown;
	subtype = 0;
	BEL = false;
}

Line::Line(QByteArray data, TagCache *tagCache)
{
	m_binary = data;
	m_plain = Terminal::interpretVt100(m_binary, Terminal::PlainText);
	
	if (tagCache != NULL) determineType(*tagCache);
}

Line::~Line()
{
}


